package com.kkb.cubemall.product.web;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.kkb.cubemall.product.service.SkuInfoService;
import com.kkb.cubemall.product.vo.SkuItemVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.PipedReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SkuItemController
 * @Description
 * @Author hubin
 * @Date 2021/5/15 18:13
 * @Version V1.0
 **/
@Slf4j
@Controller
public class SkuItemController {

    @Autowired
    private SkuInfoService skuInfoService;
    /**
     * @Description: 根据skuId查询商品详情信息
     * @Author: hubin
     * @CreateDate: 2021/5/15 18:14
     * @UpdateUser: hubin
     * @UpdateDate: 2021/5/15 18:14
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @GetMapping("/{skuId}.html")
    public String skuItem(@PathVariable Long skuId, Model model){
        // 调用服务层商品详情接口
        SkuItemVo itemVo = skuInfoService.skuItem(skuId);

        // 输入日志
        log.info("商品详情接口，查询的数据：{}",itemVo);


        // 把数据放入模型驱动
        model.addAttribute("item",itemVo);

        // 返回视图页面，做视图数据渲染
        return "item";
    }


    @GetMapping("test-flow-rule")
    @ResponseBody
    public String testFlowRule(){
        //通過定義代碼完成限流
        this.initFlowQpsRule();

        return "success";
    }

    private void initFlowQpsRule() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule("/15.html");
        // set limit qps to 20
        rule.setCount(1);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("default");
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    @GetMapping("test-sentinel-api")
    @ResponseBody
    public String testSentinelApi(@RequestParam(required = false) String a){
        // 定義資源名稱
        String resourceName = "test-sentinel-api";
        ContextUtil.enter(resourceName,"test01");

        // 定義sentinel受保護的資源
        Entry entry = null;
        try {
            entry = SphU.entry(resourceName);
            // 簡單業務
            if(StringUtils.isBlank(a)){
                throw new IllegalArgumentException("參數不能為空");
            }
            return a;

        }catch (BlockException e) {
           log.info("降級，限流了",e);
           return "降級，限流了";
        }catch (IllegalArgumentException e2){
            Tracer.trace(e2);
            return "參數非法";
        }
        finally {
            // 务必保证 exit，务必保证每个 entry 与 exit 配对
            if (entry != null) {
                entry.exit();
            }
        }


    }


    @RequestMapping("/show/err")
    public String showErr(){

        int i = 10/0;

        return null;
    }



    @SentinelResource(blockHandler = "blockHandlerGet")
    public String getStr(){
        throw new RuntimeException("get str failed！");
    }


    public String blockHandlerGet(BlockException e){
        return "降級了，限流了！";
    }


}

